<?php

class SF_CategoryPage_Helper_Auction extends Mage_Core_Helper_Abstract
{
    protected $_eventCode = null;

    public function getEvents($eventCode = null)
    {
        if($eventCode === null) return false; 
        $this->_eventCode = $eventCode;
        return $this;
    }

    public function getProducts(){
        $auction = $this->getCategory();
        $productSkus = array();
        foreach($auction as $data)
        {
            $productSkus = $data['products'];
        }
        $products = $this->getCatProducts($productSkus);
        return $products;
        
    }
    
    public function getCatProducts($productSkus){
        $products = array();        
        $product_s = Mage::getResourceModel('catalog/product_collection')
                        // ->addAttributeToSelect('*')
                        ->addAttributeToSelect('id')
                        ->addAttributeToSelect('name')
                        ->addUrlRewrite()
                        ->addAttributeToSelect('short_description')
                        ->addAttributeToSelect('thumbnail')
                        ->addAttributeToSelect('pirce')
                        ->addFinalPrice();
        $_products = $product_s->addAttributeToFilter('SKU', array('in' => $productSkus));
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_products); 

        foreach($_products as $key => $product)
        { 
        
            // if($product->getTypeId() == 'simple')
            // {
            //     $qtyStock = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
            // }
            // if($product->getTypeId() == 'configurable')
            // {
            //     $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product);
            //     $col = $conf->getUsedProductCollection()->addAttributeToSelect('status')->addFilterByRequiredOptions();
            //     $qtyStock = 0;
            //     foreach ($col as $sprod){
            //         if($sprod->getStatus() == '2') continue;
            //         $qty = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($sprod)->getQty());
            //         $qtyStock += $qty;
            //     }
            // }
            $productid = $product->getId();
            $products[$key] = array(
                    'id'             => $product->getId(),
                    'sku'            => $product->getSku(),
                    'name'           => $product->getName(),
                    'url'            => $product->getProductUrl(),
                    'thumbnail'      => (string)Mage::helper('catalog/image')->init($product, 'thumbnail')->resize('190'),
                    'sf_price'          => Mage::helper('core')->currency($this->getCatPrices($product->getSku()), true, false),
                    //'short_description' => $product->getShortDescription(),
                    //'wishlist'       => Mage::helper('wishlist')->getAddUrl($product),
                    
                    'price'          => $product->getPrice(),
                    'currency_price' => Mage::helper('core')->currency($product->getPrice(), true, false),
                    'final_price'    => $product->getFinalPrice(),
                    'currency_final_price' => Mage::helper('core')->currency($product->getFinalPrice(), true, false),
                    //'qty'            => $qtyStock,
                    // 'available'       => $product->isAvailable(),
            );
        }
        return $products;
    }
    public function getCatPrices($sku){
        $auction = $this->getCategory();
        foreach($auction as $data)
        {
            $productSkus = $data['products'];
        }
        foreach ($productSkus as $key => $value) {
            if($sku == $value){
                $price = $key;
            }
        }
        return $price;
    }
    public function begintime($codes){
        $auction = $this->getCategory();
        foreach ($auction as $key => $value) {
            $auctions = $value['begintime'];
        }
        return $auctions;
    }

    public function getCategory(){
        $auction = array(
            /*'2016/09/15'=>array(
                'begintime'=>'2016/09/18 09:00:00',
                'products'=>array(
                    "1.42"=>'020S00008_MS01410',"0.42"=>'769S00002_YD032',"1.02"=>'571S00002_LQ5012',
                ),
            ),*/

            '2016/09/19'=>array(
                'begintime'=>'2016/09/19 09:00:00',
                'products'=>array(
                    "1.42"=>'755S00017_YXF07132',
                    "2.1"=>'755S00024_RCS45G',
                    /*"0.91"=>'769S00006_sdt001-1',*/
                    "0.67"=>"769S00006_xyd006",
                ),
            ),
            '2016/09/21'=>array(
                'begintime'=>'2016/09/21 09:00:00',
                'products'=>array(
                    /*"2.03"=>'755S00024_RCS102WQ',*/
                    "0.1"=>'579S00005_163437201',
                    "0.58"=>'755S00072_Xiaomi-USB-LED',
                    "2.1"=>"755S00024_RCS45G",
                ),
            ),
            '2016/09/23'=>array(
                'begintime'=>'2016/09/23 09:00:00',
                'products'=>array(
                    "0.1"=>'755S00072_Xiaomi-TypeC',
                    "2.53"=>'755S00024_RCS41WQ',
                    "1.02"=>'755S00017_YXF07522',
                ),
            ),
            '2016/09/26'=>array(
                'begintime'=>'2016/09/26 09:00:00',
                'products'=>array(
                    "1.18" =>'769S00006_xyd016',
                    "2.86"  =>'020S00006_ME120',
                    "0.1"=>'595S00003_BD-8046',
                ),
            ),
            '2016/09/28'=>array(
                'begintime'=>'2016/09/28 09:00:00',
                'products'=>array(
                   "1.49"=>'517S00001_FANYI-39',
                    "0.94"=>'755S00025_PA3692',
                    "2.97"=>'755S00024_RCS115GQ',                  
                ),
            ),
            '2016/09/30'=>array(
                'begintime'=>'2016/09/30 09:00:00',
                'products'=>array(
                    "0.48" =>'769S00006_usbxyd003',
                    "2.7" =>'755S00059_Joway-PokemonGo01',
                    "0.1" =>'517S00001_FANYI-18',
                ),
            ),
            '2016/10/03'=>array(
                'begintime'=>'2016/10/03 09:00:00',
                'products'=>array(
                    "1.19" =>'769S00006_xyd002',
                    "0.25" =>'755S00022_GE-EX0061-U',
                    "1.18" =>'769S00006_xyd019',
                ),
            ),
            '2016/10/06'=>array(
                'begintime'=>'2016/10/06 09:00:00',
                'products'=>array(
                    "0.56"=>'574S00004_BA12002',
                    "1.29"=>'755S00019_xpz-techkey-SILVER-8GB',
                    "0.67" =>'769S00006_xyd006',
                ),
            ),
        );

        date_default_timezone_set('Europe/Moscow');
        $thisDate = date("Y/m/d");
        $keys = array_keys($auction);
        $values = array();
        foreach ($keys as $value) {
           if(strtotime($thisDate) >= strtotime($value)){
                $values[] = $value;
           }
        }

        $collkey = count($values);
        if(empty($collkey)){
            return array_slice($auction,0,1);
        }else {
            $collkey = $collkey-1;
            return array_slice($auction,$collkey,1);
        }
    }
}